<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名，多个姓名请,号隔开"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['business:user-auth:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
            type="danger"
            plain
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
            v-hasPermi="['business:user-auth:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55" />
<!--      <el-table-column label="ID" align="center" prop="id" />-->
<!--      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="部门ID" align="center" prop="deptId" />-->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="身份证" align="center" prop="idCard" width="200px"/>
      <el-table-column label="手机号" align="center" prop="mobile" width="130px"/>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="{row}">
          <el-tag v-if="row.status === 0" type="danger">未完成</el-tag>
          <el-tag v-else type="success">已完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="success"
            @click="showDetail(scope.row)"
            v-hasPermi="['business:user-auth:update']"
          >
            详情
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['business:user-auth:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['business:user-auth:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <UserAuthForm ref="formRef" @success="getList" />

  <el-drawer
    v-model="detailShow">
    <el-descriptions :column="1" label-width="170px" border>
      <el-descriptions-item label="姓名" >
        <template #label>
          <p style="color: #8a909c;display: flex; align-items: center; font-size: 14px"><Icon size="17" icon="ep:user-filled" class="mr-3px"/>姓名</p>
        </template>
        {{detail.name}}
      </el-descriptions-item>
      <el-descriptions-item label="身份证">
        <template #label>
          <p style="color: #8a909c;display: flex; align-items: center; font-size: 14px"><Icon size="17" icon="fa-solid:address-card" class="mr-3px"/>身份证</p>
        </template>
        {{detail.idCard}}
      </el-descriptions-item>
      <el-descriptions-item label="手机号">
        <template #label>
          <p style="color: #8a909c;display: flex; align-items: center; font-size: 14px"><Icon size="17" icon="fa-solid:mobile-alt" class="mr-3px"/>手机号</p>
        </template>
        {{detail.mobile}}
      </el-descriptions-item>
      <el-descriptions-item label="状态">
        <template #label>
          <p style="color: #8a909c;display: flex; align-items: center; font-size: 14px"><Icon size="17" icon="ep:switch" class="mr-3px"/>状态</p>
        </template>
        <el-tag v-if="detail.status === 0" type="danger">未完成</el-tag>
        <el-tag v-else type="success">已完成</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="创建时间">
        <template #label>
          <p style="color: #8a909c;display: flex; align-items: center; font-size: 14px"><Icon size="17" icon="ep:alarm-clock" class="mr-3px"/>创建时间</p>
        </template>
        {{detail.createTime}}
      </el-descriptions-item>
      <el-descriptions-item label="备注">
        <template #label>
          <p style="color: #8a909c;display: flex; align-items: center; font-size: 14px"><Icon size="17" icon="ep:document" class="mr-3px"/>备注</p>
        </template>
        {{detail.remark}}
      </el-descriptions-item>
      <el-descriptions-item label="位置信息">
        <template #label>
          <p style="color: #8a909c;display: flex; align-items: center; font-size: 14px"><Icon size="17" icon="ep:map-location" class="mr-3px"/>位置信息</p>
        </template>
        {{detail.location}}
      </el-descriptions-item>
      <el-descriptions-item label="授权结果">
        <template #label>
          <p style="color: #8a909c;display: flex; align-items: center; font-size: 14px"><Icon size="17" icon="fa:buysellads" class="mr-3px"/>授权结果</p>
        </template>
        <el-tag v-if="detail.isAuth">已授权</el-tag>
        <el-tag type="danger" v-else>未授权</el-tag>
      </el-descriptions-item>
    </el-descriptions>
  </el-drawer>
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import {dateFormatter, formatDate} from '@/utils/formatTime'
import download from '@/utils/download'
import { UserAuthApi, UserAuth } from '@/api/business/userauth'
import UserAuthForm from './UserAuthForm.vue'

/** 用户授权维护 列表 */
defineOptions({ name: 'UserAuth' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<UserAuth[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    if (queryParams.name){
      const data = await UserAuthApi.getUserAuthPage(queryParams)
      list.value = data.list
      total.value = data.total
    }
  } finally {
    loading.value = false
  }
}


let detailShow = ref(false)
let detail = ref({})
const showDetail = async (info) => {
  detail.value = info
  detailShow.value = true;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
  list.value = []
  total.value = 0
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UserAuthApi.deleteUserAuth(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除用户授权维护 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await UserAuthApi.deleteUserAuthList(checkedIds.value);
    checkedIds.value = [];
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: UserAuth[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UserAuthApi.exportUserAuth(queryParams)
    download.excel(data, '用户授权维护.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
